package com.study.sbsummary.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Properties;

@Slf4j
public class SqlUtil {
    public static final String DRIVE_KEY = "driverClassName";
    public static final String URL_KEY = "jdbc-url";
    public static final String USERNAME_KEY = "username";
    public static final String PASSWD_KEY = "password";

    public static DataSource shadowDataSource(Properties properties) {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName(properties.getProperty(DRIVE_KEY));
        dataSourceBuilder.url(shadowUrl(properties.getProperty(URL_KEY)));
        dataSourceBuilder.password(PASSWD_KEY).username(USERNAME_KEY);
        return dataSourceBuilder.build();
    }

    public static String shadowUrl(String url) {
        int lastQuestionMark = url.lastIndexOf('?');
        int lastBackslash = url.lastIndexOf('/');
        String dbName = url.substring(lastBackslash + 1, lastQuestionMark);
        StringBuilder sb = new StringBuilder();
        sb.append(url, 0, lastBackslash + 1);
        sb.append(url.substring(lastQuestionMark));
        log.info("shadow database url: {}", sb);
        return sb.toString();
    }

    public static SqlSessionFactory sessionFactory(DataSource mainDataSource,
                                                   AbstractRoutingDataSource proxyDataSource, String mapperLocations,
                                                   Interceptor[] interceptors, boolean isDatabase) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration configuration = new MybatisConfiguration();
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (interceptors != null && interceptors.length > 0) {
            bean.setPlugins(interceptors);
        } else {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        }
        configuration.addInterceptor(interceptor);
        bean.setConfiguration(configuration);
        if (isDatabase) {
            bean.setDataSource(proxyDataSource);
        } else {
            bean.setDataSource(mainDataSource);
        }
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(mapperLocations));
        return bean.getObject();

    }
}
